

let activity_tag = 'mt'
let cookies_all_key =  activity_tag+'_cookies_all'
let request_key = activity_tag+'_award'
let skill_count_key = activity_tag+'_award_count'
let skill_name = 'skill_name'
let skill_result = false

function rewrite_body(body) {
    console.log(body);
    if (body.subCodeMsg){
        body.subCodeMsg = "成功抓到请求";
    }
}  

function resp_case(request_value,res) {
    console.log('请求结果：');
    console.log(res.headers.date);
    console.log(res.data);
}
function error_case(request_value,e) {
    console.log(e.message)
    // axios_skill(request_value)
}
function collect_request() {

    let  cookies_all = $store.get(cookies_all_key);
    let  uuid = $request.headers.uuid;
    console.log('尝试收集请求：'+uuid);
    if (!uuid||uuid==""){
        return
    }
    if(!cookies_all||cookies_all==""){
        cookies_all = []
        $request.bodyBytes = []
        cookies_all.push($request)
    }else {
        let cookies_all_str = JSON.stringify(cookies_all)
        if(!cookies_all_str.includes(uuid)){
            $request.bodyBytes = []
            cookies_all.push($request)
        }
    }
    $store.set(cookies_all_key, cookies_all)  

}


//上面是暂时需要适配的部分
!(async () => {
    if (typeof $request !== "undefined") {
  console.log(`开始获取请求参数`);
   get_request_key();
   collect_request();
   let body = $response.body;
    body =  JSON.parse(body);
    rewrite_body(body);
    body =  JSON.stringify(body);
    $done({ body })
}else {
    console.log(`开始执行请求`);
    let request_value = $store.get(request_key);
    if(request_value&&request_value!=""){
        //  $store.delete没有暴露出来，无法使用，兼容下
        // $store.set(request_key, "")  
        setTimeout(axios_skill,700,request_value,count);
        setTimeout(axios_skill,950,request_value,);
        setTimeout(axios_skill,1100,request_value);
        // axios_skill(request_value,count);
        
    }else{
        console.log(request_key+`不存在`);
    }
    }
    })()
   .catch((e) =>console.log(e))
   .finally(() =>$done())

function axios_skill(request_value){
        console.log(`执行请求：`);
        $axios(request_value)
        .then(res=>resp_case(request_value,res))
        .catch(e=>error_case(request_value,e));  
}


function get_request_key() {
    $store.set(request_key, $request) 
}
